import { isEmpty,isPhone } from '../../utils/util';
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    avatarUrl: '',
    nickName: '',
    mobile: '',
    checkCode:'',
    showUserInfo: false,
    appServicer:"15840661234",
    countDown:60,
    setTime:null
  },

  lifetimes: {
    attached: function () {
      let userInfo = wx.getStorageSync('userInfo');
      if (userInfo) {
        if(userInfo.mobile){
          this.setData({
            avatarUrl: userInfo.avatarUrl,
            nickName: userInfo.nickName,
            mobile: userInfo.mobile
          });
          console.log("获取用户积分")
        }else{
          this.setData({
            avatarUrl: userInfo.avatarUrl,
            nickName: userInfo.nickName
          });
        }
      } else {
        console.log('获取用户信息')
      }
    },
    moved: function () { },
    detached: function () { },
  },
  attached: function () { },
  ready: function () { },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {},
    hide: function () { },
    resize: function () { },
  },

  methods: {
    getUserProfile(){//点我绑定身份
      this.setData({
        showUserInfo:true
      });
      // wx.getUserProfile({
      //   desc: '推荐个性化产品, 参与会员活动',
      //   success: (res) => {
      //     let userInfo=res.userInfo;
      //     wx.setStorageSync("userInfo", userInfo);
      //     this.setData({
      //       avatarUrl: userInfo.avatarUrl,
      //       nickName: userInfo.nickName,
      //     });
      //     this.requestChangeUserInfo();
      //   },
      //   fail:(err)=> {
      //       console.log('授权失败', err);
      //   }
      // })
    },
    inputMobile(e){
      this.data.mobile=e.detail.value;
    },
    inputNickName(e){
      this.data.nickName=e.detail.value;
    },
    inputCheckCode(e){
      this.data.checkCode=e.detail.value;
    },
    //通过手机号获取验证码
    getCheckCode(){
      if(this.data.countDown!=60){
        return;
      }
      if(isEmpty(this.data.mobile)){
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if(!isPhone(this.data.mobile)){
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      console.log('调用获取验证码接口');
    },
    //绑定手机号
    postBindMobile(e) {
      if(isEmpty(this.data.nickName)){
        wx.showToast({
          title: '请输入您的用户名',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if(isEmpty(this.data.mobile)){
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if(!isPhone(this.data.mobile)){
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if(isEmpty(this.data.checkCode)){
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      console.log('调用注册接口完成注册');
    },
    toMyCare() {
      
    },
    toMyScore(){
     
    },
    callServicerPhone() {
      wx.makePhoneCall({
        phoneNumber: this.data.appServicer,
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
    toFeedback() {
      
    },
    toAbout() {
      
    },
    toMyOrder() {
      
    },
    onClickHead: function () {
      let _self = this;
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          _self.setData({
            "headUrl": tempFilePaths[0]
          });
          wx.uploadFile({
            url: app.globalData.requestUrl + '/appupload/avatar', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            success: function (res) {
              let obj=JSON.parse(res.data);
              _self.setData({
                "avatarUrl": app.globalData.imgUrlHead + obj.data
              });

            }
          })
        }
      })
    },
    logOut(){
      this.setData({
        avatarUrl: "",
        nickName: "",
        mobile: ""
      });
      wx.setStorageSync('userInfo', '');
    },
  },
})
